package com.lujianfei.virusbroadcast.ui;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;

import com.lujianfei.lib.dialog.BaseDialog;
import com.lujianfei.lib.sdautolayout.impl.SDAutoLayout;
import com.lujianfei.lib.widgets.JLabelEdit;
import com.lujianfei.virusbroadcast.cache.SPRepository;

public class SettingDialog extends BaseDialog {

	JLabelEdit originalCount;
	JLabelEdit broadRate;
	JLabelEdit shadowTime;
	JLabelEdit hospitalReceiveTime;
	JLabelEdit bedCount;
	JLabelEdit mu;
	JLabelEdit cityPersonSize;
	JLabelEdit fatalityRate;
	JLabelEdit dieTime;
	JLabelEdit dieVariance;
	JButton confirm;
	JButton cancel;
	List<JComponent> mLabelEdits = new ArrayList<JComponent>();
	JComponent lastComp = null;
	ActionListener onConfirmListener;

	public void setOnConfirmListener(ActionListener onConfirmListener) {
		this.onConfirmListener = onConfirmListener;
	}

	public SettingDialog(Frame owner, boolean modal) {
		super(owner, modal);
		setTitle("设置");
		setSize(250, 580);
		originalCount = new JLabelEdit("初始感染数量:", "" + SPRepository.getInstance().getOriginalCount());
		broadRate = new JLabelEdit("传播率:", "" + SPRepository.getInstance().getBroadRate());
		shadowTime = new JLabelEdit("潜伏时间(14天为140):", "" + SPRepository.getInstance().getShadowTime());
		hospitalReceiveTime = new JLabelEdit("医院收治响应时间:",
				"" + SPRepository.getInstance().getHospitalReceiveTime());
		bedCount = new JLabelEdit("医院床位:", "" + SPRepository.getInstance().getBedCount());
		mu = new JLabelEdit("流动意向平均值[-0.99,0.99]:", "" + SPRepository.getInstance().getMU());
		cityPersonSize = new JLabelEdit("城市总人口数量:", "" + SPRepository.getInstance().getCityPersonSize());
		fatalityRate = new JLabelEdit("病死率:", "" + SPRepository.getInstance().getFatalityRate());
		dieTime = new JLabelEdit("死亡时间均值:", "" + SPRepository.getInstance().getDieTime());
		dieVariance = new JLabelEdit("死亡时间方差:", "" + SPRepository.getInstance().getDieVariance());
		mLabelEdits = Arrays.asList(originalCount, broadRate, shadowTime, hospitalReceiveTime, bedCount, mu,
				cityPersonSize, fatalityRate, dieTime, dieVariance);
		for (int i = 0; i < mLabelEdits.size(); i++) {
			JComponent jComponent = mLabelEdits.get(i);
			add(jComponent);
			if (null == lastComp) {
				SDAutoLayout.forComponent(jComponent).widthIs(getWidth()).heightIs(35).leftSpaceToComponent(this, 5)
						.topSpaceToComponent(this, 10);
			} else {
				SDAutoLayout.forComponent(jComponent).widthIs(getWidth()).heightIs(35).leftSpaceToComponent(this, 5)
						.topSpaceToComponent(lastComp, 10);
			}
			lastComp = jComponent;
		}

		confirm = new JButton("确定");
		cancel = new JButton("取消");
		add(confirm);
		add(cancel);

		SDAutoLayout.forComponent(confirm).widthIs(100).heightIs(35).bottomSpaceToComponent(this, 40)
				.leftSpaceToComponent(this, 5);
		SDAutoLayout.forComponent(cancel).widthIs(confirm.getWidth()).heightIs(confirm.getHeight())
				.bottomSpaceToComponent(this, 40).leftSpaceToComponent(confirm, 5);

		confirm.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SPRepository.getInstance().setOriginalCount(originalCount.getTextEdit().toInt());
				SPRepository.getInstance().setBroadRate(broadRate.getTextEdit().toFloat());
				SPRepository.getInstance().setShadowTime(shadowTime.getTextEdit().toFloat());
				SPRepository.getInstance().setHospitalReceiveTime(hospitalReceiveTime.getTextEdit().toInt());
				SPRepository.getInstance().setBedCount(bedCount.getTextEdit().toInt());
				SPRepository.getInstance().setMU(mu.getTextEdit().toFloat());
				SPRepository.getInstance().setCityPersonSize(cityPersonSize.getTextEdit().toInt());
				SPRepository.getInstance().setFatalityRate(fatalityRate.getTextEdit().toFloat());
				SPRepository.getInstance().setDieTime(dieTime.getTextEdit().toInt());
				SPRepository.getInstance().setDieVariance(dieVariance.getTextEdit().toDouble());
				exit();
				if (null != onConfirmListener) {
					onConfirmListener.actionPerformed(e);
				}
			}
		});

		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				exit();
			}
		});
	}

}
